package com.usaas.search.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.PlatUserPublishDto;
import com.usaas.search.enums.PlatUserEnums;
import com.usaas.search.enums.PlatUserPublishEnums;
import com.usaas.search.exception.ExceptionUtil;
import com.usaas.search.mapper.PlatUserPublishMapper;
import com.usaas.search.pojo.PlatUserPojo;
import com.usaas.search.pojo.PlatUserPublishPojo;
import com.usaas.search.service.IPlatUserPublishService;
import com.usaas.search.service.IPlatUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户游戏发布Service业务层处理
 *
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatUserPublishServiceImpl extends ServiceImpl<PlatUserPublishMapper, PlatUserPublishPojo> implements IPlatUserPublishService {

    @Resource
    private PlatUserPublishMapper platUserPublishMapper;
    @Autowired
    private IPlatUserService platUserService;

    /**
     * 查询用户游戏发布
     *
     * @param id 用户游戏发布主键
     * @return 用户游戏发布
     */
    @Override
    public PlatUserPublishDto queryById(Long id) {
        PlatUserPublishPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatUserPublishDto dto = BeanUtil.copyProperties(pojo, PlatUserPublishDto.class);
            List<PlatUserPublishDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询用户游戏发布列表
     *
     * @param platUserPublishDto 用户游戏发布
     * @return 用户游戏发布
     */
    @Override
    public List<PlatUserPublishDto> queryList(PlatUserPublishDto platUserPublishDto) {
        LambdaQueryWrapper<PlatUserPublishPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = this.buildWrapper(queryWrapper, platUserPublishDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatUserPublishPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatUserPublishDto> dtoList = BeanUtil.copyToList(list, PlatUserPublishDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatUserPublishDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return;
        }
    }

    /**
     * 新增用户游戏发布
     *
     * @param platUserPublishDto 用户游戏发布
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatUserPublishDto platUserPublishDto) {
        PlatUserPublishPojo platUserPublishPojo = BeanUtil.copyProperties(platUserPublishDto, PlatUserPublishPojo.class);
        return this.save(platUserPublishPojo);
    }

    /**
     * 新增用户游戏发布
     *
     * @param platUserPublishDto 用户游戏发布
     * @return 结果
     */
    @Override
    public void appAdd(PlatUserPublishDto platUserPublishDto) {
        //判断是否有待审核的,是否禁止发布了
        LambdaQueryWrapper<PlatUserPublishPojo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatUserPublishPojo::getUserId, platUserPublishDto.getUserId());
        wrapper.eq(PlatUserPublishPojo::getPassStatus, PlatUserPublishEnums.PASS_STATUS_PENDINGREVIEW.getValueLong());
        long count = this.count(wrapper);
        if (count > 0) {
            ExceptionUtil.throwError("当前存在待审核的信息");
        }
        PlatUserPojo userPojo = platUserService.getById(platUserPublishDto.getUserId());
        Long publishStatus = userPojo.getPublishStatus();
        if (publishStatus.equals(PlatUserEnums.PUBLISHSTATUS_FORBID.getValueLong())) {
            ExceptionUtil.throwError("您已被禁止发布");
        }
        PlatUserPublishPojo platUserPublishPojo = BeanUtil.copyProperties(platUserPublishDto, PlatUserPublishPojo.class);
        this.save(platUserPublishPojo);
    }

    /**
     * 修改用户游戏发布
     *
     * @param platUserPublishDto 用户游戏发布
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatUserPublishDto platUserPublishDto) {
        PlatUserPublishPojo platUserPublishPojo = BeanUtil.copyProperties(platUserPublishDto, PlatUserPublishPojo.class);
        return this.updateById(platUserPublishPojo);
    }

    /**
     * 批量删除用户游戏发布
     *
     * @param ids 需要删除的用户游戏发布主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }

    @Override
    public AjaxResult check(PlatUserPublishDto platUserPublishDto) {
        Long passStatus = platUserPublishDto.getPassStatus();
        Assert.notNull(passStatus, "审核状态不能为空");
        PlatUserPublishPojo dbInfo = this.getById(platUserPublishDto.getId());
        if (ObjectUtil.equal(passStatus, PlatUserPublishEnums.PASS_STATUS_POSTEDMALICIOUSLY.getValueLong())) {
            //設置用戶为禁止发布
            Long userId = dbInfo.getUserId();
            PlatUserPojo platUserPojo = new PlatUserPojo();
            platUserPojo.setId(userId);
            platUserPojo.setPublishStatus(PlatUserEnums.PUBLISHSTATUS_FORBID.getValueLong());
            platUserService.updateById(platUserPojo);
        }
        PlatUserPublishPojo platUserPublishPojo = BeanUtil.copyProperties(platUserPublishDto, PlatUserPublishPojo.class);
        this.updateById(platUserPublishPojo);
        return AjaxResult.success();
    }


    /**
     * 分页查询${subTable.functionName}信息
     *
     * @param platUserPublishDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatUserPublishDto platUserPublishDto) {
        Page<PlatUserPublishPojo> page = new Page<>(platUserPublishDto.getPageNum(), platUserPublishDto.getPageSize());
        LambdaQueryWrapper<PlatUserPublishPojo> wrapper = new LambdaQueryWrapper<PlatUserPublishPojo>();
        //此处可做前置判断
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        wrapper = buildWrapper(wrapper, platUserPublishDto);
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        Page<PlatUserPublishPojo> pageResult = this.page(page, wrapper);
        Page<PlatUserPublishDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatUserPublishPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatUserPublishDto> dtos = BeanUtil.copyToList(records, PlatUserPublishDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    private LambdaQueryWrapper<PlatUserPublishPojo> buildWrapper(LambdaQueryWrapper<PlatUserPublishPojo> wrapper, PlatUserPublishDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
        Long id = param.getId();
        if (id != null) {
            wrapper.eq(PlatUserPublishPojo::getId, id);
        }
        Long userId = param.getUserId();
        if (userId != null) {
            wrapper.eq(PlatUserPublishPojo::getUserId, userId);
        }
        String name = param.getName();
        if (StrUtil.isNotBlank(name)) {
            wrapper.eq(PlatUserPublishPojo::getName, name);
        }
        Long categoryId = param.getCategoryId();
        if (categoryId != null) {
            wrapper.eq(PlatUserPublishPojo::getCategoryId, categoryId);
        }
        String suggestDesc = param.getSuggestDesc();
        if (StrUtil.isNotBlank(suggestDesc)) {
            wrapper.eq(PlatUserPublishPojo::getSuggestDesc, suggestDesc);
        }
        String detailDesc = param.getDetailDesc();
        if (StrUtil.isNotBlank(detailDesc)) {
            wrapper.eq(PlatUserPublishPojo::getDetailDesc, detailDesc);
        }
        String downloadDesc = param.getDownloadDesc();
        if (StrUtil.isNotBlank(downloadDesc)) {
            wrapper.eq(PlatUserPublishPojo::getDownloadDesc, downloadDesc);
        }
        Long passStatus = param.getPassStatus();
        if (passStatus != null) {
            wrapper.eq(PlatUserPublishPojo::getPassStatus, passStatus);
        }
        String passReason = param.getPassReason();
        if (StrUtil.isNotBlank(passReason)) {
            wrapper.eq(PlatUserPublishPojo::getPassReason, passReason);
        }
        String createBy = param.getCreateBy();
        if (StrUtil.isNotBlank(createBy)) {
            wrapper.eq(PlatUserPublishPojo::getCreateBy, createBy);
        }
        Date createTime = param.getCreateTime();
        if (createTime != null) {
            wrapper.eq(PlatUserPublishPojo::getCreateTime, createTime);
        }
        String updateBy = param.getUpdateBy();
        if (StrUtil.isNotBlank(updateBy)) {
            wrapper.eq(PlatUserPublishPojo::getUpdateBy, updateBy);
        }
        Date updateTime = param.getUpdateTime();
        if (updateTime != null) {
            wrapper.eq(PlatUserPublishPojo::getUpdateTime, updateTime);
        }
        String remark = param.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            wrapper.eq(PlatUserPublishPojo::getRemark, remark);
        }
        Long delFlag = param.getDelFlag();
        if (delFlag != null) {
            wrapper.eq(PlatUserPublishPojo::getDelFlag, delFlag);
        }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
